import { asyncRouterMap, constantRouterMap } from '@/router/index';
import { login } from '@/api/manage/menu.js'
import router from '../../router';
import store from '../index'

const getComponent = function(pathList, routerList){
  let componentLsit = []
  componentLsit = getComponentList(pathList, routerList, componentLsit)
  return  componentLsit
}

const getComponentList  = function(pathList, routerList, componentLsit){
  for (let i = 0; i < routerList.length; i++) {
    const ele = routerList[i];
    for (let j = 0; j < pathList.length; j++) {
      const path = pathList[j];
      if (path == ele.path){
        componentLsit[path] = ele.component
      }
      
    }
    if (ele.children && ele.children.length > 0){
      getComponentList(pathList, ele.children, componentLsit)
    }
    
  }
  return  componentLsit
}


const getPath = function(rules, menuList){
  let pathList = []
  if(rules != '*'){
    getPathItem(rules, menuList ,pathList)
  } else {
    getAllPath(menuList ,pathList)
  }
  return pathList

}
const getAllPath = function(menuList ,pathList){
  for (let i = 0; i < menuList.length; i++) {
    const ele = menuList[i];
    pathList.push(ele.path)
    if(ele.childlist && ele.childlist.length > 0){
      getAllPath(ele.childlist, pathList)
    }
  }
}
const getPathItem = function(rules, menuList ,pathList){
  for (let i = 0; i < menuList.length; i++) {
    const ele = menuList[i];
    if(rules.indexOf(ele.id) > -1){
      pathList.push(ele.path)
    }
    if(ele.childlist && ele.childlist.length > 0){
      getPathItem(rules, ele.childlist ,pathList)
    }
  }
} 

const hasPermission = function(data,pathList, componentList){
  if(!data){
    return;
  }
  data.forEach(ele => {
    ele.meta = {
      title: ele.name,
      icon: ele.icon,
      hidden: true
    }
    let childlist = ele.childlist
    if (childlist && ele.childlist.length > 0) {
      if (childlist[0].is_menu == 1){
        ele.children = ele.childlist
      }
    }
    if(pathList.indexOf(ele.path) == -1 || ele.is_menu == 0){
      ele.meta.hidden = true
    } else {
      ele.meta.hidden = false
      ele.component = componentList[ele.path]
    }
    ele.children && hasPermission(ele.children, pathList, componentList)
  });
  return data
}
const permission = {
  state:{
    routers: constantRouterMap,
    addRouters: [], // 
    userInfo: {}, // 用户信息
    menuList: [], //菜单列表 所有的已开启的菜单
    rule: '', // 当前用户拥有的菜单的id
    ruleList: [], // 当用户的菜单
  },
  mutations:{
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = constantRouterMap.concat(routers);
    },
    SET_USERINFO: (state, userinfo) => {
      state.userInfo = userinfo;
    },
    SET_MENULIST: (state, menuList) => {
      state.menuList = menuList
    },
    SET_RULE: (state, rule) =>{
      state.rule = rule
    },
    SET_RULELIST: (state, ruleList) =>{
      localStorage.setItem('ruleList', ruleList)
      state.ruleList = ruleList
    }
    

  },
  actions:{
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        login().then(res=>{
          let rules = ''
          if(res.status == 200){
            let data = res.data 
            // let menuIconArr =[]
            let ruleList = data.ruleList
            if(ruleList){   
              commit('SET_MENULIST', ruleList);
              // for (let i = 0; i < ruleList.length; i++) {
              //   menuIconArr[ruleList[i].path] = ruleList[i].icon
              // }
            }
            // console.log(menuIconArr)
            if(data.userInfo){
              commit('SET_USERINFO', data.userInfo);
            }
            if(data.menuInfo && data.menuInfo.rules){
              commit('SET_RULE', data.menuInfo.rules);
              rules = data.menuInfo.rules
            }
            let pathList = getPath(rules, ruleList)
            localStorage.setItem('permission',JSON.stringify(pathList))

            commit('SET_RULELIST', pathList);
            // console.log(pathList)
            // if(menuIconArr){
            //   asyncRouterMap.filter(item => {
            //     item.meta.icon = menuIconArr[item.path]
            //   })
            // }
            let componentsList = getComponent(pathList, asyncRouterMap)
            // console.log(componentsList)
            const accessedRouters = hasPermission(ruleList,pathList,componentsList)
            // let path = '/welcome'
            // let child = accessedRouters[0].children
            // if(child && child.length > 0){
            //   path = child[0].path
            // }
            // console.log(accessedRouters)
            // router.push({path:path})
            let to = router.currentRoute
            if (pathList.indexOf(to.path) > -1){
              let obj = {
                data: to,
                isRemove:false 
              }
              store.dispatch('SetSelectMenu',obj)
            }
           
            commit('SET_ROUTERS', accessedRouters);

          } 
        }) 
        resolve();
      })
    }
  }
}
export default permission